﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using UIWindows.Commands;
using UIWindows.Models.Devices;
using XingLucifer.IBase;
using XingLucifer.IBase.Enums;
using XingLucifer.Devices.Inits;
using static NewLife.Remoting.ApiHttpClient;

namespace UIWindows.ViewModels.DeviceConfig
{
    public class DeviceInitVModel : UIWindows.Domains.ViewModelBase, IDeviceViewModel
    {
        private List<IDeviceInit> _deviceList = new List<IDeviceInit>();
        private DeviceClientModel _DCModel;
        private string? _message;
        /// <summary>
        /// 配置日志
        /// </summary>
        public string? LogMessage
        {
            get { return _message; }
            set { SetProperty(ref _message, value); }
        }

        public DeviceClientModel DCModel
        {
            get { return _DCModel; }
            set { SetProperty(ref _DCModel, value); }
        }
        private string _source;
        /// <summary>
        /// 源IP
        /// </summary>
        public string Source
        {
            get { return _source; }
            set { SetProperty(ref _source, value); }
        }
        public IEnumerable<DeviceTypes> Foods
        {
            get
            {
                foreach (DeviceTypes c in (DeviceTypes[])Enum.GetValues(typeof(DeviceTypes)))
                {
                    yield return c;
                }
            }
        }
        private DeviceTypes _deviceType;

        public DeviceTypes DeviceType
        {
            get { return _deviceType; }
            set { SetProperty(ref _deviceType, value); }
        }

        public DeviceInitVModel()
        {
            _deviceList = new List<IDeviceInit>
            {
                new TC06(AddLog),
                new UT6604(AddLog),
                new UT6608(AddLog),
                new FeatherInjectionPump(AddLog)
            };
        }

        public void SendModel(DeviceClientModel device)
        {
            if (device.TaskType == TaskTypes.扫描信号)
            {
                return;
            }
            DCModel = device;
            DeviceType = DCModel.DeviceType;
            switch (DCModel.DeviceType)
            {
                case DeviceTypes.RJ6902R_ShortCircuit:
                case DeviceTypes.ST5520_ShortCircuit:
                case DeviceTypes.AND4531B_Scale:
                case DeviceTypes.UT6601:
                case DeviceTypes.UT6602:
                case DeviceTypes.UT6604:
                case DeviceTypes.UT6608:
                    Source = "192.168.1.125";
                    break;
                case DeviceTypes.SR700:
                case DeviceTypes.SR1000:
                    Source = "192.168.100.100";
                    break;
                case DeviceTypes.TC06_Scale:
                    Source = "192.168.16.254";
                    break;
                case DeviceTypes.NJ_FINS_PLC:
                    Source = "192.168.250.1";
                    break;
                case DeviceTypes.S715_PLC:
                    break;
                case DeviceTypes.S712_PLC:
                    break;
                default:
                    break;
            }
        }

        private void AddLog(string str) => LogMessage += $"{str}\r\n";

        public DelegateCommand<Button> InitDeviceCommand => new DelegateCommand<Button>(sender => {
            LogMessage = null;
            sender.IsEnabled = false;
            _ = Task.Run(() =>
            {
                IDeviceInit? deviceInit = _deviceList.FirstOrDefault(x => x.Name == DeviceType);
                try
                {
                    if (deviceInit == null)
                    {
                        AddLog("该设备未创建配置工具");
                        return;
                    }
                    if (!deviceInit.Ping(Source))
                    {
                        AddLog("设备Ping不通,请检测网线");
                        return;
                    }
                    AddLog("设备在线");
                    if (!deviceInit.Init(Source, DCModel.IPCOM, "255.255.255.0", DCModel.Port))
                    {
                        AddLog("设备配置失败！！！");
                        return;
                    }
                    AddLog("配置完成");
                    if (!deviceInit.Reboot())
                    {
                        AddLog("重启失败！！！");
                        return;
                    }
                    AddLog("重启完成");
                    if (!deviceInit.Ping(DCModel.IPCOM))
                    {
                        AddLog("配置失败");
                        return;
                    }
                    AddLog("配置成功");
                }
                catch (Exception ex)
                {
                    AddLog(ex.ToString());
                }
            }).ContinueWith(x => sender.Dispatcher.Invoke(() => sender.IsEnabled = true)).ConfigureAwait(false);
        });
    }
}
